<?php

namespace app\admin\model;

class AdminAdmin extends ModelBase
{
    protected $table = 'admin_admin';
    protected $deleteTime = 'delete_time';

    /**
     * 用户登录验证
     */
    public function login($data)
    {
        $where = [
            'username' => trim($data['username']),
            'password' => set_password(trim($data['password'])),
            'status'   => 1,
        ];
        $token = md5key(60);
        //验证用户
        $admin = self::where($where)->find();
        if (!$admin) {
            return false;
        }
        //验证成功
        $admin->token = $token;
        $admin->save();
        $data = [
            'id'       => $admin->id,
            'token'    => $admin->token,
            'username' => $admin->username,
            'nickname' => $admin->nickname,
            'menu'     => $this->permissions($admin->id),
        ];
        session(['admin' => $data]);
        return $admin;
    }

    /**
     * 用户的所有权限
     */
    public function permissions($id)
    {
        $admin = self::with('roles')->find($id);
        $permissions = [];
        //超级管理员缓存所有权限
        if ($admin['id'] == 1) {
            $perms = AdminPermission::order('sort', 'asc')->select()->toArray();
            foreach ($perms as $p) {
                if ($p['status'] == 1) {
                    $permissions[$p['id']] = [
                        'id'    => $p['id'],
                        'pid'   => $p['pid'],
                        'title' => $p['title'],
                        'href'  => APP_ADMIN.$p['href'],
                        'icon'  => 'layui-icon '.$p['icon'],
                        'type'  => $p['type'],
                        'sort'  => $p['sort'],
                    ];
                }
            }
        } else {
            //处理权限
            if (isset($admin['roles']) && !empty($admin['roles'])) {
                foreach ($admin['roles'] as $r) {
                    if ($r['permissions']) {
                        $permission = AdminPermission::whereIn('id', $r['permissions'])->select()->toArray();
                        foreach ($permission as $p) {
                            if ($p['status'] == 1) {
                                $permissions[$p['id']] = [
                                    'id'    => $p['id'],
                                    'pid'   => $p['pid'],
                                    'title' => $p['title'],
                                    'href'  => APP_ADMIN.$p['href'],
                                    'icon'  => 'layui-icon '.$p['icon'],
                                    'type'  => $p['type'],
                                    'sort'  => $p['sort'],
                                ];
                            }
                        }
                    }
                }
            }
            $key = array_column($permissions, 'sort');
            array_multisort($key, SORT_ASC, $permissions);
        }
        //合并权限为用户的最终权限
        return $permissions;
    }

    /**
     * 判断是否登录
     * @return bool|array
     */
    public function isLogin()
    {
        $admin = session('admin');
        if (empty($admin)) {
            return false;
        }
        if (empty($admin['id']) || empty($admin['token'])) {
            return false;
        }
        $info = self::where(['id' => $admin['id'], 'token' => $admin['token'], 'status' => 1])->find();
        if (empty($info)) {
            return false;
        }
        return true;
    }

    /**
     * 退出登陆
     * @return bool
     */
    public function logout()
    {
        session(['admin' => null]);
//        Response()->cookie('hash', null);
        return true;
    }

    /**
     * 管理拥有的角色
     */
    public function roles()
    {
        return $this->belongsToMany('AdminRole', 'admin_admin_role', 'role_id', 'admin_id');
    }

    /**
     * 检查验证码
     */
    public function captchaCheck($captcha)
    {
        // 对比session中的captcha值
        if (strtolower($captcha) !== session('captcha')) {
            return false;
        }
        return true;
    }

    /**
     * 更新菜单权限
     */
    public function upMenu()
    {
        $admin = session('admin');
        $admin['menu'] = $this->permissions($admin['id']);
        session(['admin' => $admin]);
        return $admin;
    }
}
